package com.wan.data.resource.parser.impl;

import cn.hutool.core.text.csv.CsvData;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvRow;
import cn.hutool.core.text.csv.CsvUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * csv解析器
 * csv编码必须是UTF-8，其他编码中文会出现乱码现象
 * @author wwh
 * @date 2022/7/6 11:52
 */
public class CsvParser extends ExcelParser{

    @Override
    public String[] suffixes() {
        return new String[]{"csv","CSV"};
    }

    @Override
    protected List<List<Object>> read(File file) {
        List<List<Object>> list = new ArrayList<>();
        CsvReader reader = CsvUtil.getReader();
        CsvData csvData = reader.read(file);
        for (CsvRow row : csvData.getRows()) {
            List<Object> collect = row.getRawList().stream().map(r -> (Object) r).collect(Collectors.toList());
            list.add(collect);
        }
        return list;
    }


}
